package com.jbm.jbmsupplier.model;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import android.os.Environment;
import android.text.format.DateFormat;

import com.jbm.jbmsupplier.util.GuidCreatorUtil;

public class fileInfo
{
	static private Map<String,fileInfo> filesmap = new HashMap<String, fileInfo>();
	public final static int TYPE_TEXT = 1;
	public final static int TYPE_PIC = 2;
	public final static int TYPE_SOUND = 3;
	public final static int TYPE_VIDEO = 4;
	public final static int TYPE_INDEX_XML=5;
	
	private String fileName="";
	private int type = 0;
	private String pathName="";
	private String text="";
	private String date="";
	private String id="";
	private String dec="";
	private JobInfo parentInfo = null;
	public fileInfo(JobInfo info,int fileType) {
		this.parentInfo = info;
		this.type = fileType;
		date = DateFormat.format("yyyy-MM-dd:hh:mm", new Date()).toString(); 
		info.addFileInfo(fileInfo.this);
		id = new GuidCreatorUtil().toString();
		filesmap.put(id, fileInfo.this);
	}
	
	public static fileInfo getFileInfoByID(String id)
	{
		return filesmap.get(id);
	}
	
	public void Remove(){
		filesmap.remove(this);
	}
	
	public String getId() {
		return id;
	}
	
	public String getDate() {
		return date;
	}
	public String getFileName() {
		return getFileNameString();
	}
	public String getPathName() {
		return getFilePath();
	}
	public String getText() {
		return text;
	}
	public int getType() {
		return type;
	}
	
	public String getFileType()
	{
		switch(this.type)
		{
		case TYPE_TEXT:
			return "txt";
		case TYPE_PIC:
			return "pic";
		case TYPE_SOUND:
			return "audio";
		case TYPE_VIDEO:
			return "vedio";
		}
		return "";
	}
	
	public String getFileExt()
	{
		switch(this.type)
		{
		case TYPE_TEXT:
			return "";
		case TYPE_PIC:
			return ".jpg";
		case TYPE_SOUND:
			return ".mp3";
		case TYPE_VIDEO:
			return ".mp4";
		}
		return "";
		
	}
	public String getFullFileName()
	{
		return getFilePath()+getFileNameString();
	}
	public void setDate(String date) {
		this.date = date;
	}
	public void setDate() {
		SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");     
		this.date   =   sDateFormat.format(new java.util.Date());		
	}

	public void setText(String text) {
		this.text = text;
	}
	
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	
	public void setPathName(String pathName) {
		this.pathName = pathName;
	}
	

	private String getFilePath()
	{
		if(this.pathName.equals(""))
		{
			File skRoot = Environment.getExternalStorageDirectory();
			this.pathName = skRoot.getPath()+ File.separator+"mediafileInfo"+File.separator+
					this.parentInfo.getGuid() + File.separator;

			File file = new File(this.pathName);
			if(!file.exists())
			{
				file.mkdirs();
			}
		}
		return this.pathName;
	}
	
	private String getFileNameString() {
        String fname = DateFormat.format("yyyyMMddhhmmss", new Date()).toString();  
		
		if(this.fileName.equals(""))
		{
			this.fileName = fname;
			switch (this.type) {
			case TYPE_PIC:
				this.fileName +=".jpg";
				break;
			case TYPE_SOUND:
				this.fileName +=".mp3";
				break;
			case TYPE_VIDEO:
				this.fileName +=".mp4";
				break;
			case TYPE_INDEX_XML:
				this.fileName = "index.xml";
			default:
				break;
			}
		}
		return this.fileName;
	}

	public String getDec() {
		return dec;
	}

	public void setDec(String dec) {
		this.dec = dec;
	}
}
